package servlet;


import entity.Employee;
import service.EmployeeService;
import service.Impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/Employee")
public class EmployeeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null) {
            method = "findAll_employee";
        }
        switch (method) {
            // 查询所有员工信息
            case "findAll_employee":
                EmployeeService showEmployeeService = new EmployeeServiceImpl();
                ArrayList<Employee> employeeList = null;
                employeeList = showEmployeeService.getEmployeeList();
                req.setAttribute("EmployeeList", employeeList);
                req.getRequestDispatcher("jsp/employee.jsp").forward(req, resp);
                break;
            // 接受添加请求，跳转到添加员工界面
            case "add":
                resp.sendRedirect("jsp/addEmployee.jsp");
                break;
            //添加
            case "addEmployee":
                Integer eid = Integer.valueOf(req.getParameter("employeeId"));
                String name = req.getParameter("employeeName");
                Integer age = Integer.valueOf(req.getParameter("age"));
                String job = req.getParameter("job");
                String addr = req.getParameter("addr");
                String phone = req.getParameter("phone");
                String wechat = req.getParameter("wechat");
                String mail = req.getParameter("mail");
                Employee employee = new Employee(eid,name,age,job,addr,phone,wechat,mail);
                EmployeeService employeeService1 = new EmployeeServiceImpl();
                //判断是否添加成功
                boolean isAdd = employeeService1.addEmployee(employee);
                // 如果成功添加，返回员工列表界面
                if (isAdd) {
                    resp.sendRedirect("Employee");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 接受更新请求，跳转到更改界面
            case "update":
                Integer employeeId = Integer.valueOf(req.getParameter("employeeId"));
                EmployeeService employeeService = new EmployeeServiceImpl();
                Employee employee1 = employeeService.findById(employeeId);
                req.setAttribute("employee", employee1);
                req.getRequestDispatcher("jsp/updateEmployee.jsp").forward(req,resp);
                break;
            // 修改某个员工信息
            case "updateEmployee":
                // 获取员工信息
                eid = Integer.valueOf(req.getParameter("employeeId"));
                name = req.getParameter("employeeName");
                age = Integer.valueOf(req.getParameter("age"));
                job = req.getParameter("job");
                addr = req.getParameter("addr");
                phone = req.getParameter("phone");
                wechat = req.getParameter("wechat");
                mail = req.getParameter("mail");
                Employee employee2 = new Employee(eid,name,age,job,addr,phone,wechat,mail);
                EmployeeService employeeService2 = new EmployeeServiceImpl();
                // 判断是否修改成功
                boolean isUpdate = employeeService2.updateEmployee(employee2);
                // 如果成功修改，返回员工列表界面
                if (isUpdate) {
                    resp.sendRedirect("Employee");
                }else { // 修改失败，跳转到错误界面
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;
            // 根据员工编号删除员工信息
            case "delete":
                Integer employeeId2 = Integer.valueOf(req.getParameter("employeeId"));
                EmployeeService employeeService3 = new EmployeeServiceImpl();
                boolean isDelete = employeeService3.deleteEmployee(employeeId2);
                if (isDelete) {
                    resp.sendRedirect("Employee");
                } else {
                    resp.sendRedirect("jsp/error.jsp");
                }
                break;


            default:
                break;
        }

    }

}
